<!-- 券专区商品详情 -->
<template>
	<view class="container ">
		<view class="carousel">
			<swiper :autoplay="true" interval="3000" circular=true duration="400">
				<block v-for="(item,index) in storeInfo.slider_image" :key="index">
					<swiper-item class="swiper-item">
						<view class="image-wrapper">
							<easy-loadimage mode="widthFix" :scroll-top="scrollTop" :image-src="item"></easy-loadimage>
						</view>
					</swiper-item>
				</block>
			</swiper>
		</view>

		<view class="introduce-section">
			<view class="pd-30">
				<view class="title">
					{{storeInfo.store_name||""}}
				</view>
				<view class="price-box">
					<text class="price">{{goodsPrice}}</text>
					<view class="exchangeBtn" @tap="toggleSpec">马上兑换</view>
				</view>
			</view>
		</view>
		<view class="cut"></view>
		<view class="detail-desc">
			<view class="txt">商品描述</view>
			<rich-text class="rich-text" :nodes="description"></rich-text>
		</view>
		<view class="cut"></view>
		<view class="exchangePro">
			<view class="title">兑换流程</view>
			<view class="content" v-for="(item,index) in exchangePro" :key="index">
				{{item}}
			</view>
		</view>
		<!-- 规格-模态层弹窗 -->
		<view class="popup spec" :class="specClass" @touchmove.stop.prevent="stopPrevent" @click="toggleSpec">
			<!-- 遮罩层 -->
			<view class="mask"></view>
			<view class="layer attr-content" @click.stop="stopPrevent">
				<view>
					<view class="a-t">
						<image lazy-load :src="selectDisObj.image"></image>
						<view class="right">
							<text class="price">{{selectDisObj.price}}</text>
							<!-- <text class="stock">库存：{{selectDisObj.stock}}件</text> -->
							<view class="selected" v-if="selectDisObj.suk">
								已选：{{selectDisObj.suk}}
							</view>
						</view>
					</view>
					<view class="a-c">
						<view class="attr-list">
							<block v-for="(items, index) in specList" :key="index">
								<text class="specTitle">{{items.attr_name}}</text>
								<view class="itemBox">
									<view @tap='selectSpec(item,index,idx)' :class="subIndex[index] == idx ?'itemAct':''" class="item" v-for="(item,idx) in items.attr_value"
									 :key="idx">{{item.attr}}</view>
								</view>
							</block>
						</view>
						<!-- :max="item.productInfo.attrInfo.stock" -->
					</view>
					<view class="num">
						<text>数量</text>
						<uni-number-box class="uni-number-box" :min="1" :value="goodsNum" @eventChange="numberChange"></uni-number-box>
					</view>
					<view class="btn-box">
						<button class="btn" @click="submit">马上兑换</button>
					</view>
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	import uniNumberBox from '@/components/uni-number-box/uni-number-box.vue'
	import utils from "@/common/utils.js"
	import {
		ticketIePd,
		ticketAddCart
	} from '@/api/mall.js'
	import {
		formatRichText
	} from '@/utils/util.js'
	export default {
		components: {
			uniNumberBox,
		},
		data() {
			return {
				storeInfo: {}, //商品信息
				slider_image: [], //轮播图
				specClass: 'none', //规格弹窗开关
				specList: [], //规格列表
				selectArr: [], // 选择的规格
				selectDisObj: { //选择的规格对应产品信息
					price: 0,
				},
				goodsNum: 1, //选择的商品数量
				sku: '', //选中的规格索引
				unique: null, //选中规格标识
				btnType: 0,
				description: '', //商品详情
				seller_info: {
					seller_type: 1,
				}, //商家信息
				reply: {}, //评价信息
				data: {},
				subIndex: [], //选中规格索引
				goodsPrice: 0, //商品价格
				exchangePro: [], //兑换流程
				scrollTop: 0
			};
		},
		onLoad(options) {
			ticketIePd({
				p_id: options.id,
			}).then(res => {
				this.data = res.data
				this.storeInfo = res.data.store_info
				this.slider_image = res.data.store_info.slider_image
				if (res.data.productAttr.length !== 0) {
					this.specList = res.data.productAttr
				}
				this.goodsPrice = res.data.store_info.price
				this.selectDisObj = res.data.store_info
				this.exchangePro = res.data.sign_rule_remark.split("##")
				if (res.data.store_info.description) {
					this.description = formatRichText(res.data.store_info.description)
				}
			})
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},
		methods: {
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			numberChange(e) {
				this.goodsNum = e.number;
			},
			//规格弹窗开关
			toggleSpec() {
				if (this.specClass === 'show') {
					this.specClass = 'hide'
				} else if (this.specClass === 'hide' || this.specClass === 'none') {
					this.specClass = 'show'
				}
			},
			//选择规格
			selectSpec(item, index, idx) {
				if (this.selectArr[index] !== item.attr) {
					this.$set(this.selectArr, index, item.attr)
					this.$set(this.subIndex, index, idx)
					this.sku = String(this.selectArr)
					if (this.data.productValue[this.sku]) {
						this.selectDisObj = this.data.productValue[this.sku]
					}
				}
			},
			submit() {
				uni.showLoading({
					mask:true
				})
				console.log(this.selectDisObj,99908)
				ticketAddCart({
					productId: this.storeInfo.id,
					uniqueId:  this.selectDisObj.unique,
					cartNum: this.goodsNum,
					new: 1
				}).then(res => {
					uni.navigateTo({
						url:"./IntegralCreateOrder?cartId=" + res.data.cartId
					})
				})
			},
			stopPrevent() {}
		},
		onHide() {
			this.specClass = 'none'
		},
	}
</script>

<style lang='scss' scoped>
	.container {
		overflow-x: hidden;
	}

	page {
		overflow-x: hidden;
		background: $page-color-base;
		padding-bottom: 160rpx;
	}

	.cut {
		width: 100%;
		height: 20rpx;
		background: #f5f5f5;
	}

	.icon-you {
		font-size: $uni-font-size-base + 1rpx;
		color: #999;
	}

	.carousel {
		height: 450rpx;
		position: relative;

		swiper {
			height: 100%;
		}

		.image-wrapper {
			width: 100%;
			height: 100%;
		}

		.swiper-item {
			display: flex;
			justify-content: center;
			align-content: center;
			height: 450rpx;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
			}
		}
		

	}

	/* 标题简介 */
	.introduce-section {
		background: #fff;

		.pd-30 {
			padding: 38rpx 30rpx 0;

			image {
				width: 70rpx;
				height: 28rpx;
				margin-right: 12rpx;
			}
		}

		.title {
			font-weight: 500;
			color: #333333;
			font-size: 36rpx;
			/* height: 50rpx; */
			line-height: 50rpx;
			text-align: justify;
		}

		.price-box {
			display: flex;
			align-items: flex-end;
			justify-content: space-between;
			height: 64rpx;
			margin-top: 50rpx;
			padding: 0rpx 0 40rpx;
			border-bottom: 1rpx #f4f4f4 solid;
			font-size: 26rpx;

			.price {
				color: #FA6E25;
				font-size: 40rpx;
				font-weight: bold;

				&:after {
					content: " 券";
					color: #AAAAAA;
					font-weight: normal;
					font-size: 24rpx;
				}
			}
			.exchangeBtn{
				width: 160rpx;
				height: 66rpx;
				background: #81DF2D;
				border-radius: 33rpx;
				color: #fff;
				font-size: 28rpx;
				text-align: center;
				line-height: 66rpx;
			}
		}
	}

	/*  详情 */
	.detail-desc {
		background: #fff;

		.txt {
			padding:40rpx 30rpx 40rpx;
			text-align: left;
			font-size: 30rpx;
			color: #333;
			background: #fff;
		}

		.rich-text {
			width: 100%;
			overflow-x: hidden;

			.img {
				width: 100%;
			}

			image {
				width: 100%;
			}
		}
	}
	.exchangePro{
		padding:  0 30rpx;
		.title{
			font-size: 28rpx;
			line-height: 100rpx;
		}
		.content{
			padding-bottom: 50rpx;
			font-size: 26rpx;
			color: #999;
		}
	}
	/* 规格选择弹窗 */
	.attr-content {
		padding: 30rpx;
		box-sizing: border-box;

		.a-c {
			position: absolute;
			top: 150rpx;
			bottom: 210rpx;
			overflow-y: scroll;
		}

		.a-t {
			display: flex;
			align-items: flex-end;

			image {
				width: 170rpx;
				height: 170rpx;
				flex-shrink: 0;
				margin-top: -70rpx;
				border-radius: 8rpx;
			}

			.right {
				display: flex;
				flex-direction: column;
				padding-left: 24rpx;
				font-size: 20rpx;
				color: #999;
				line-height: 30rpx;

				.price {
					line-height: 40rpx;
					font-weight: bold;
					font-size: $uni-font-size-lg;
					color: #FA6E25;

					&:after {
						content: ' 券';
						font-size: 20rpx;
					}
				}
			}
		}

		.num {
			position: absolute;
			left: 0;
			bottom: 120rpx;
			height: 100rpx;
			width: 100%;
			padding: 0 30rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.uni-number-box {
				width: 200rpx;
				height: 50rpx;
				position: relative;
			}
		}

		.btn-box {
			position: absolute;
			left: 0;
			bottom: 0;
			width: 100%;
			border-top: 1px solid #eee;
			z-index: 99;
			height: 119rpx;
			display: flex;
			align-items: center;

			.btn {
				height: 76rpx;
				line-height: 76rpx;
				border-radius: 38rpx;
				background: #81DF2D;
				font-size: 28rpx;
				color: #fff;
				width: 690rpx;
			}
		}

		.attr-list {
			padding-top: 30rpx;

			.specTitle {
				font-size: 28rpx;
				color: #999;
			}

			.itemBox {
				margin-top: 30rpx;
				display: flex;
				flex-wrap: wrap;

				.item {
					font-size: 24rpx;
					height: 50rpx;
					line-height: 50rpx;
					text-align: center;
					color: #999;
					border: 1rpx solid #999;
					margin-right: 30rpx;
					padding: 0 30rpx;
					border-radius: 25rpx;
					margin-bottom: 30rpx;
				}

				.itemAct {
					background-color: #81DF2D;
					border: 1rpx solid #81DF2D;
					color: #fff;
				}
			}
		}

	}

	/*  弹出层 */
	.popup {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 99;

		&.show {
			display: block;

			.mask {
				animation: showPopup 0.2s linear both;
			}

			.layer {
				animation: showLayer 0.2s linear both;
			}
		}

		&.hide {
			animation: hideLayer 0.2s linear both;

			.mask {
				animation: hideLayer 0.2s linear both;
			}

			.layer {
				animation: hideLayer 0.2s linear both;
			}
		}

		&.none {
			display: none;
		}

		.mask {
			position: fixed;
			top: 0;
			width: 100%;
			height: 100%;
			z-index: 1;
			background-color: rgba(0, 0, 0, 0.4);
		}

		.layer {
			position: fixed;
			z-index: 99;
			bottom: 0;
			padding-bottom: env(safe-area-inset-bottom);
			width: 100%;
			height: 70vh;
			border-radius: 10rpx 10rpx 0 0;
			background-color: #fff;
		}

		@keyframes showPopup {
			0% {
				opacity: 0;
			}

			100% {
				opacity: 1;
			}
		}

		@keyframes hidePopup {
			0% {
				opacity: 1;
			}

			100% {
				opacity: 0;
			}
		}

		@keyframes showLayer {
			0% {
				transform: translateY(120%);
			}

			100% {
				transform: translateY(0%);
			}
		}

		@keyframes hideLayer {
			0% {
				transform: translateY(0);
			}

			100% {
				transform: translateY(120%);
			}
		}
	}
</style>
